home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / GC / reclaim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-25  |  4.8 KB  |  182 lines

  1. #include <stdio.h>
  2. #include "gc.h"
  3. #define DEBUG
  4. #undef DEBUG
  5. #ifdef PRINTSTATS
  6. #  define GATHERSTATS
  7. #endif
  8.  
  9. long mem_found = 0;     /* Number of longwords of memory reclaimed     */
  10.  
  11. long composite_in_use;  /* Number of longwords in accessible composite */
  12.             /* objects.                                    */
  13.  
  14. long atomic_in_use;     /* Number of longwords in accessible atomic */
  15.             /* objects.                                 */
  16.  
  17. /*
  18.  * reclaim phase
  19.  *
  20.  */
  21.  
  22. reclaim()
  23. {
  24. register struct hblk *hbp;    /* ptr to current heap block        */
  25. register int word_no;        /* Number of word in block        */
  26. register long i;
  27. register word *p;        /* pointer to current word in block    */
  28. register int mb;        /* mark bit of current word        */
  29. int sz;                /* size of objects in current block    */
  30. word *plim;
  31. struct hblk **nexthbp;        /* ptr to ptr to current heap block    */
  32. int nonempty;            /* nonempty ^ done with block => block empty*/
  33. struct obj *list;        /* used to build list of free words in block*/
  34. register int is_atomic;         /* => current block contains atomic objs */
  35.  
  36. #   ifdef DEBUG
  37.         printf("clearing all between %x and %x, %x and %x\n",
  38.                objfreelist, &objfreelist[MAXOBJSZ+1],
  39.                aobjfreelist,&aobjfreelist[MAXAOBJSZ+1]);
  40. #   endif
  41.     { register struct obj **fop;
  42.     for( fop = objfreelist; fop < &objfreelist[MAXOBJSZ+1]; fop++ ) {
  43.         *fop = (struct obj *)0;
  44.     }
  45.     for( fop = aobjfreelist; fop < &aobjfreelist[MAXAOBJSZ+1]; fop++ ) {
  46.         *fop = (struct obj *)0;
  47.     }
  48.     }
  49.     
  50.     atomic_in_use = 0;
  51.     composite_in_use = 0;
  52.  
  53. #   ifdef PRINTBLOCKS
  54.         printf("reclaim: current block sizes:\n");
  55. #   endif
  56.  
  57.   /* go through all heap blocks (in hblklist) and reclaim unmarked objects */
  58. # ifdef HBLK_MAP
  59.     hbp = (struct hblk *) heapstart;
  60.     for (; ((char *)hbp) < heaplim; hbp++) if (is_hblk(hbp)) {
  61. /* fprintf(stderr, "Reclaiming in 0x%X\n", hbp); */
  62. # else
  63.     nexthbp = hblklist;
  64.     while( nexthbp < last_hblk ) {
  65.     hbp = *nexthbp++;
  66. # endif
  67.  
  68.     nonempty = FALSE;
  69.     sz = hbp -> hb_sz;
  70.     is_atomic = 0;
  71.     if (sz < 0) {
  72.         sz = -sz;
  73.         is_atomic = 1;        /* this block contains atomic objs */
  74.     }
  75. #    ifdef PRINTBLOCKS
  76.             printf("%d(%c",sz, (is_atomic)? 'a' : 'c');
  77. #    endif
  78.  
  79.     if( sz > (is_atomic? MAXAOBJSZ : MAXOBJSZ) ) {  /* 1 big object */
  80.         mb = mark_bit(hbp, (hbp -> hb_body) - ((word *)(hbp)));
  81.         if( mb ) {
  82. #               ifdef GATHERSTATS
  83.             if (is_atomic) {
  84.             atomic_in_use += sz;
  85.             } else {
  86.             composite_in_use += sz;
  87.             }
  88. #               endif
  89.         nonempty = TRUE;
  90.         } else {
  91.         mem_found += sz;
  92.         }
  93.     } else {                /* group of smaller objects */
  94.         p = (word *)(hbp->hb_body);
  95.         word_no = ((word *)p) - ((word *)hbp);
  96.         plim = (word *)((((unsigned)hbp) + HBLKSIZE)
  97.                - WORDS_TO_BYTES(sz));
  98.  
  99.         list = (is_atomic) ? aobjfreelist[sz] : objfreelist[sz];
  100.  
  101.       /* go through all words in block */
  102.         while( p <= plim )  {
  103.         mb = mark_bit(hbp, word_no);
  104.  
  105.         if( mb ) {
  106. #                   ifdef GATHERSTATS
  107.             if (is_atomic) atomic_in_use += sz;
  108.             else           composite_in_use += sz;
  109. #                   endif
  110. #                   ifdef DEBUG
  111.                         printf("found a reachable obj\n");
  112. #            endif
  113.             nonempty = TRUE;
  114.             p += sz;
  115.         } else {
  116.           mem_found += sz;
  117.           /* word is available - put on list */
  118.             ((struct obj *)p)->obj_link = list;
  119.             list = ((struct obj *)p);
  120.           if (is_atomic) {
  121.             p += sz;
  122.           } else {
  123.             /* Clear object, advance p to next object in the process */
  124.             i = (long)(p + sz);
  125.                         p++; /* Skip link field */
  126.                         while (p < (word *)i) {
  127.                 *p++ = 0;
  128.             }
  129.           }
  130.         }
  131.         word_no += sz;
  132.         }
  133.  
  134.       /*
  135.        * if block has reachable words in it, we can't reclaim the
  136.        * whole thing so put list of free words in block back on
  137.        * free list for this size.
  138.        */
  139.         if( nonempty ) {
  140.         if ( is_atomic )    aobjfreelist[sz] = list;
  141.         else            objfreelist[sz] = list;
  142.         }
  143.     } 
  144.  
  145. #    ifdef PRINTBLOCKS
  146.             printf("%c),", nonempty ? 'n' : 'e' );
  147. #    endif
  148.     if (!nonempty) {
  149.             if (!is_atomic && sz <= MAXOBJSZ) {
  150.                 /* Clear words at beginning of objects */
  151.                 /* Since most of it is already cleared */
  152.           p = (word *)(hbp->hb_body);
  153.           plim = (word *)((((unsigned)hbp) + HBLKSIZE)
  154.              - WORDS_TO_BYTES(sz));
  155.           while (p <= plim) {
  156.             *p = 0;
  157.             p += sz;
  158.           }
  159.         hbp -> hb_uninit = 0;
  160.         } else {
  161.         /* Mark it as being uninitialized */
  162.         hbp -> hb_uninit = 1;
  163.         }
  164.  
  165.       /* remove this block from list of active blocks */
  166.         del_hblklist(hbp);    
  167.  
  168. #           ifndef HBLK_MAP
  169.           /* This entry in hblklist just got replaced; look at it again  */
  170.           /* This admittedly depends on the internals of del_hblklist... */
  171.           nexthbp--;
  172. #           endif
  173.  
  174.         freehblk(hbp);
  175.     }  /* end if (one big object...) */
  176.     } /* end while (nexthbp ...) */
  177.  
  178. #   ifdef PRINTBLOCKS
  179.         printf("\n");
  180. #   endif
  181. }
  182.